<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="会议名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="会议名称"></el-input>
			</el-form-item>
			<el-form-item label="会议室" prop="pid">
				<el-tree-select
					v-model="dataForm.pid"
					:data="chamberList"
					value-key="id"
					check-strictly
					clearable
					:render-after-expand="false"
					:props="{ value: 'id', label: 'name', children: 'children' }"
					placeholder="会议室"
				/>
			</el-form-item>
			<el-form-item label="会议开始时间" prop="startDatetime">
				<ElDatePicker v-model="dataForm.startDatetime" placeholder="请选择日期" value="yyyy-MM-dd" />
				<ElDatePicker
					v-model="dataForm.endDatetime"
					range-separator="To"
					type="timerange"
					start-placeholder="开始时间"
					end-placeholder="结束时间"
					value="HH:mm:ss"
				/>
			</el-form-item>

			<el-form-item label="会议提醒" prop="remindId">
				<el-tree-select
					v-model="dataForm.remindId"
					:data="dictList"
					value-key="id"
					check-strictly
					clearable
					:render-after-expand="false"
					:props="{ value: 'code', label: 'name', children: 'children' }"
					placeholder="会议提醒"
				/>
			</el-form-item>
			<el-form-item label="附件" prop="fileUrl">
				<el-upload :action="constant.uploadUrl" :before-upload="beforeUpload" :on-success="handleSuccess" :show-file-list="false">
					<el-button type="primary">上传</el-button>
				</el-upload>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { Ref, reactive, ref } from 'vue'
import { ElMessage, UploadProps } from 'element-plus/es'
import { useChamberlogApi, useChamberlogSubmitApi } from '@/api/nb/chamber/chamber_log'
import constant from '@/utils/constant'
import { findListByDictId } from '@/api/nb/business/dictionaries_subset'

const emit = defineEmits(['refreshDataList'])
defineProps(['chamberList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	name: '',
	pid: '',
	startDatetime: '',
	endDatetime: '',
	remindId: '',
	fileUrl: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getChamber(id)
	}

	getDictList('DaiBanTiXing', dictList)
}

const getChamber = (id: number) => {
	useChamberlogApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

//列表树
const dictList = ref()
const getDictList = (code: String, list: Ref) => {
	findListByDictId({ params: { code: code } }).then(res => {
		list.value = res.data
		list.value.unshift({ code: '', name: '请选择' })
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useChamberlogSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

//上传附件
const fileDataForm = reactive({
	name: '',
	platform: '',
	size: '',
	url: ''
})

const handleSuccess: UploadProps['onSuccess'] = (res, file) => {
	if (res.code !== 0) {
		ElMessage.error('上传失败：' + res.msg)
		return false
	}

	Object.assign(fileDataForm, res.data)

	dataForm.fileUrl = fileDataForm.url
}

const beforeUpload: UploadProps['beforeUpload'] = file => {
	if (file.size / 1024 / 1024 / 1024 / 1024 > 1) {
		ElMessage.error('文件大小不能超过100M')
		return false
	}
	return true
}

defineExpose({
	init
})
</script>
